#include "Common/Common.h"
#include "EditorCore/Traversers/ParentsFirstEntityTraverser.h"

using namespace cmn;

namespace editor_core {

	void ParentsFirstEntityTraverser::InnerTraverse( IEntity* entity, IEntityVisitor* visitor, std::set<int>& visitedEntities ) {
		if( visitedEntities.find( entity->GetId() ) != visitedEntities.end() )
			return;

		Vector<IEntity*> parents;
		entity->GetParents( parents );

		for( IteratorPtr<IEntity*> parentsIter( parents );
				!parentsIter.IsEnd();
				parentsIter.Next() )
		{
			IEntity* parent = parentsIter.Current();
			if( visitedEntities.find( parent->GetId() ) == visitedEntities.end() )
				return;
		}

		visitor->VisitEntity( entity );
		visitedEntities.insert( entity->GetId() );

		Vector<IEntity*> children;
		entity->GetChildren( children );

		for( IteratorPtr<IEntity*> childrenIter( children );
				!childrenIter.IsEnd();
				childrenIter.Next() )
		{
			InnerTraverse( childrenIter.Current(), visitor, visitedEntities );
		}
	}

}
